/**
 * 文件名    ： CorsFilter.java
 * 版权       : XX科技有限公司。
 * 描述       : <描述>
 * 修改时间：2017年5月21日
 * 修改内容：<修改内容>
 */
package com.example.demo.application.common.intercept;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

import com.example.demo.application.common.MyConstants;
import com.example.demo.application.common.MyConstantsRespone;
import com.example.demo.application.common.MyJsonUtils;
import com.example.demo.application.common.base.ResultMsg;
import com.example.demo.application.myredis.RedisHelper;  
  
/** 
 *  
 *  跨域过滤器  ---(WebMvcConfigurer 类  过滤器 有问题，不再使用 WebMvcConfigurer + MyUrlInterceptor 组合)
 * @author meng 
 * @version  
 * @since 2016年6月19日 
 */  
///@WebFilter(filterName="myFilter",urlPatterns="/**")
@Component
public class MyCorsFilter implements Filter {  
  
    final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MyCorsFilter.class);  
  
    /* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
        HttpServletResponse response = (HttpServletResponse) res;  
        HttpServletRequest request = (HttpServletRequest) req;  
        
//        response.setHeader("Access-Control-Allow-Origin", "*");  
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));  
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
        response.setHeader("Access-Control-Max-Age", "3600");  
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,x-token-authority,Content-Type"); 
     
		String url = request.getRequestURL().toString();
		logger.debug(url);
		//需要授权的链接：包含 update add  priviges ， 进逻辑
		///noauth swagger webjars v2  druid 不用授权
		if (url.indexOf("update") > 0 || url.indexOf("add") > 0 || url.indexOf("priviges") > 0) {
			/*
			 * String userToken = request.getHeader(MyConstants.TOKEN_AUTH); if (null !=
			 * userToken && !"null".equals(userToken)) { String s =
			 * RedisHelper.get(MyConstants.LOGIN_DENCY + userToken); //异常的 异地登录 if
			 * (MyConstants.LOGIN_DENCY_STATUS.equals(s)) {
			 * response.setHeader("Content-type", "text/html;charset=UTF-8");//乱码问题
			 * response.getOutputStream().write(MyJsonUtils.toJSON(LoginExcept.
			 * LOGIN_EXCEPT_MSG).getBytes("UTF-8")); return ; } // header 转移
			 * response.setHeader(MyConstants.TOKEN_AUTH, userToken); }else {
			 * response.setHeader("Content-type", "text/html;charset=UTF-8");//乱码问题
			 * response.getOutputStream().write(MyJsonUtils.toJSON(LoginExcept.LOGIN_NOT_MSG
			 * ).getBytes("UTF-8")); return ; }
			 */
		}
		
        logger.debug("*********************************过滤器被使用**************************");  
        chain.doFilter(req, res);  
    }  
    
    public void init(FilterConfig filterConfig) {}  
    public void destroy() {}  
	static class LoginExcept {
		/** 登录异常：5  */
		public static final ResultMsg LOGIN_EXCEPT_MSG = new ResultMsg(MyConstantsRespone.LOGIN_EXCEPT,"账号异地登录","");
		/** 登录异常：5  */
		public static final ResultMsg LOGIN_NOT_MSG = new ResultMsg(MyConstantsRespone.LOGIN_EXCEPT,"请登录","");
	}

}  